from FileReader import FileReader
from Constant import *
import os

class FileSaver():
    
    def __init__(self, fileReader, outputFileName):
        self.__001_initialInstanceVariable(fileReader, outputFileName)
    
    def __001_initialInstanceVariable(self, fileReader, outputFileName):
        #assign pointer of file reader
        self.fileReader = fileReader
        #name of directory to save
        self.dir_save = DIR_SAVE_DIR
        #name of output file name
        self.outputFileName = outputFileName
        
    def __002_saveSubFile_startPoint(self):
        #create file
        fileName = self.dir_save + "/" + FILENAME_START_PTS
        fp = file(fileName, 'w')
        #write content to file
        list_traj = self.fileReader.GET001_list_trajInfo()
        index = 0
        while index < list_traj.__len__() :
            list_point = list_traj[index]
            fp.write("%d %d %d" % ((index + 1), list_point[0].x, list_point[0].y))
            if index < list_traj.__len__() - 1 :
                fp.write("\n")
            index = index + 1
        fp.close()
    
    def __003_saveSubFile_endPoint(self):
        #create file
        fileName = self.dir_save + "/" + FILENAME_END_PTS
        fp = file(fileName, 'w')
        #write content to file
        list_traj = self.fileReader.GET001_list_trajInfo()
        index = 0
        while index < list_traj.__len__() :
            list_point = list_traj[index]
            fp.write("%d %d %d" % ((index + 1), list_point[list_point.__len__() - 1].x, 
                                    list_point[list_point.__len__() - 1].y))
            if index < list_traj.__len__() - 1 :
                fp.write("\n")
            index = index + 1
        fp.close()
    
    def __004_saveSubFile_segmant(self):
        list_traj = self.fileReader.GET001_list_trajInfo()
        fileName = ""
        index_traj = 0
        while index_traj < list_traj.__len__() :
            #create file
            fileName = FILENAME_SEGMANT
            fileName = fileName.replace("0", "%d" % (index_traj + 1))
            fileName = self.dir_save + "/" + fileName
            fp = file(fileName, 'w')
            #write content to file
            list_point = list_traj[index_traj]
            index_point = 0
            while index_point < list_point.__len__() :
                fp.write("%d %d %d %s" % (list_point[index_point].num, 
                                          list_point[index_point].x,
                                          list_point[index_point].y,
                                          list_point[index_point].timeLabel))
                if index_point < list_point.__len__() - 1 :
                    fp.write("\n")
                index_point = index_point + 1
            fp.close()
            index_traj = index_traj + 1
    
    def __005_saveSubFile_plot(self):
        list_traj = self.fileReader.GET001_list_trajInfo()
        #set sample file of plot
        file_sample = file(DIR_READ_FILE + "/" + FILENAME_PLOT)
        #create new file of plot
        file_new = file(DIR_SAVE_DIR + "/" + FILENAME_PLOT, 'w')
        #load content of sample file
        sampleData = file_sample.read()
        file_sample.close()
        #set content of arrow
        content_arrow = ""
        index_traj = 0
        while index_traj < list_traj.__len__() :
            list_point = list_traj[index_traj]
            index_point = 0
            while index_point < list_point.__len__() - 1 :
                content_arrow = content_arrow + "set arrow from " + \
                                "%d,%d to %d,%d" % (list_point[index_point].x, 
                                                    list_point[index_point].y,
                                                    list_point[index_point + 1].x, 
                                                    list_point[index_point + 1].y)
                content_arrow = content_arrow + "\n"
                index_point = index_point + 1
            index_traj = index_traj + 1
        #set content of segmentation
        content_seg = "plot " + \
                      "\"" + FILENAME_START_PTS + "\"" + TRAJ_NOTATION + ",\\" + "\n" \
                      "\"" + FILENAME_END_PTS + "\"" + TRAJ_NOTATION + ",\\" + "\n"
        index_seg = 0
        while index_seg < list_traj.__len__() :
            fileName_seg = FILENAME_SEGMANT
            fileName_seg = fileName_seg.replace("0", "%d" % (index_seg + 1))
            content_seg = content_seg + "\"" + fileName_seg + "\"" + TRAJ_NOTATION
            if index_seg < list_traj.__len__() - 1 :
                content_seg = content_seg + ",\\" + "\n"
            index_seg = index_seg + 1
        #replace content
        sampleData = sampleData.replace(OUTPUT_REPLACE, self.outputFileName)
        sampleData = sampleData.replace(TRAJ_REPLACE_ARROW, content_arrow)
        sampleData = sampleData.replace(TRAJ_REPLACE_PLOT, content_seg)
        file_new.write(sampleData)
        file_new.close()
    
    def __006_checkDir(self):
        if os.path.isdir(self.dir_save) :  #if directory exists, clear all original data
            self.__007_rmdir(DIR_SAVE_DIR, False)
        else : #if not exist, create a new directory
            os.mkdir(self.dir_save)
            
    def __007_rmdir(self, dir_name, isRmDir):
        list = os.listdir(dir_name)
        fileName = ""
        index = 0
        while index < list.__len__() :
            fileName = dir_name + "/" + list[index]
            if os.path.isfile(fileName) :
                os.remove(fileName)
            else :
                self.__007_rmdir(fileName, True)
            index = index + 1
        if isRmDir :
            os.rmdir(dir_name)
            
    def ACT001_saveFile(self):
        self.__006_checkDir()
        self.__002_saveSubFile_startPoint()
        self.__003_saveSubFile_endPoint()
        self.__004_saveSubFile_segmant()
        self.__005_saveSubFile_plot()
        